<!-- vue 模板 -->
<template>
  <div id="marketIndex">
    <div class="topBox">
      <div class="top">
        <div>极速</div>
        <div @click="to('/GJmarket')">合约</div>
      </div>
    </div>
    <div class="name">
      <div @click="showPopup">{{ title }} <Icon name="arrow-down" /></div>
      <div @click="to('/topspeed')"><Icon name="description" />持仓</div>
    </div>
    <iframe :src="'/html/index.html?symbol=' + symbol" frameborder="0"></iframe>
    <div class="dataBtn">
      <div @click="to('/tradeRecord', 0)">实时数据</div>
      <div @click="to('/tradeRecord', 1)">实时成交</div>
    </div>
    <div class="bottom">
      <Checkbox v-model="checked" shape="square">取消自选</Checkbox>
      <div class="btn">
        <div class="green">买多</div>
        <div>卖空</div>
      </div>
    </div>
    <Tabbar></Tabbar>
    <Popup v-model="show" position="left">
      <div class="symbolBox">
        <div class="title">货币行情</div>
        <Tabs class="tabBox" v-model="active">
          <Tab :title="item" v-for="item in tabItem" :key="item" :text="item" />
        </Tabs>
        <Search v-model="value" placeholder="搜索币种" />
        <div class="datasBox">
          <div
            class="datas"
            v-for="(item, index) in symbolsArray"
            :key="index"
            @click="clickTab(item)"
          >
            <span>{{ item.title }}</span>
            <span
              :class="
                ch[`market.${item.symbol}.detail`].change > 0 ? 'green' : 'red'
              "
              v-if="ch[`market.${item.symbol}.detail`]"
              >{{ ch[`market.${item.symbol}.detail`].tick.close }}</span
            >
          </div>
        </div>
      </div>
    </Popup>
  </div>
</template>

<script>
import Tabbar from '@/components/Tabbar.vue'
import { Icon, Checkbox, Popup, Tab, Tabs, Search } from 'vant'

import { EventBus } from '@/utils/event-bus'

export default {
  components: {
    Tabbar,
    Icon,
    Checkbox,
    Popup,
    Tab,
    Tabs,
    Search
  },
  data() {
    return {
      // 币种列表
      symbolsArray: [
        { symbol: 'btcusdt', title: 'BTC/USDT' },
        { symbol: 'ethusdt', title: 'ETH/USDT' },
        { symbol: 'eosusdt', title: 'EOS/USDT' },
        { symbol: 'crousdt', title: 'CRO/USDT' },
        { symbol: 'trxusdt', title: 'TRX/USDT' }
      ],
      ch: {},
      symbol: 'btcusdt', //币种symbol
      title: 'BTC/USDT',
      checked: false,
      show: false,
      tabItem: ['自选', 'USDT'],
      active: 0, //tab数值
      value: '' //搜索内容
    }
  },
  created() {
    let Interval = setInterval(() => {
			if (this.$store.state.websocket.is_open_socket) {
				this.loadTopMarket();
				// 已连接移除定时器
				clearInterval(Interval);
			}
		}, 500);
    console.log(this.$route.query, '------------------------')

   if (localStorage.getItem('symbol')=='undefined') {
			localStorage.setItem('symbol', JSON.stringify({ symbol: 'btcusdt', title: 'BTCUSDT' }));
			this.clickTab(JSON.parse(localStorage.getItem('symbol')));
			return
		}
		if (localStorage.getItem('symbol')) {
			this.clickTab(JSON.parse(localStorage.getItem('symbol')));
			return
		}
		if (this.$route.query.symbol) {
			localStorage.setItem('symbol', JSON.stringify(this.$route.query.symbol));
			this.clickTab(JSON.parse(localStorage.getItem('symbol')));
			return;
		}
		if (!this.$route.query.symbol) {
			localStorage.setItem('symbol', JSON.stringify({ symbol: 'btcusdt', title: 'BTCUSDT' }));
			this.clickTab(JSON.parse(localStorage.getItem('symbol')));
		}
  },
  mounted() {},
  methods: {
    //  切换b种
    clickTab(symbol) {
      this.symbol = symbol.symbol
      this.title = symbol.title
      this.show = false
      this.symbolsArray.forEach(item => {
					this.$set(this.ch, `market.${item.symbol}.detail`,this.$store.state.websocket.ch[`market.${item.symbol}.detail`]);
				});
    },
    showPopup() {
      this.show = true
    },
    to(to, type) {
      this.$router.push({
        path: to,
        query: {
          symbol: this.$route.query.symbol,
          type: type || 0
        }
      })
    },
    //  最新市场概要快照
    loadTopMarket() {
      this.symbolsArray.forEach(item => {
        let ch = `market.${item.symbol}.detail`
        // 消息回调
        EventBus.$on(ch, res => {
          if (!res.data) return
          let d = res.data
          d.change = parseFloat(
            ((d.tick.close - d.tick.open) / d.tick.open) * 100
          ).toFixed(2)
          this.$set(this.ch, res.data.ch, d)
          // console.log(this.ch)
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
#marketIndex {
  color: #fff;
  background: #181b2a;
  min-height: 100vh;
  .topBox {
    overflow: hidden;
    .top {
      margin: 0 auto;
      margin-top: 6px;
      display: flex;
      align-items: center;
      line-height: 32px;
      width: 100px;
      background: #293047;
      justify-content: space-between;
      border: solid 1px #e5e5e5;
      div {
        width: 50%;
        text-align: center;
      }
      div:nth-child(1) {
        background: #c8c7cc;
      }
    }
  }
  .name {
    margin-top: 10px;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    div:nth-child(1) {
      display: flex;
      align-items: center;
      i {
        margin-left: 4px;
        color: #999;
      }
    }
    div:nth-child(2) {
      display: flex;
      align-items: center;
      font-size: 14px;
      i {
        margin-right: 2px;
      }
    }
  }
  iframe {
    width: 100vw;
    height: calc(100vh - 218px);
  }
  .dataBtn {
    position: fixed;
    left: 50%;
    margin-left: -75px;
    bottom: 122px;
    display: flex;
    width: 150px;
    justify-content: space-between;
    div {
      color: #2483ff;
      border: 1px solid #383f66;
      padding: 10px;
    }
  }
  .bottom {
    width: 95%;
    position: fixed;
    bottom: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    border-top: 1px solid #fff;
    /deep/.van-checkbox {
      .van-checkbox__label {
        margin-top: 2px;
        color: #fff;
      }
    }
    .btn {
      display: flex;
      div {
        width: 70px;
        line-height: 38px;
        padding: 0 5px;
        font-size: 14px;
        color: #fff;
        border-radius: 500px;
        border: none;
        text-align: center;
        background-color: #cd4e65;
      }
      div:nth-child(1) {
        margin-right: 5px;
      }
      .green {
        background: #1cad90;
      }
    }
  }
  .symbolBox {
    width: 220px;
    height: 100vh;
    color: #000;
    .title {
      line-height: 30px;
      padding: 10px;
      border-bottom: solid 1px #e6eaed;
    }
    .tabBox {
      /deep/.van-tabs__wrap {
        margin: 10px 0;
      }
      /deep/.van-tab--active {
        font-size: 15px;
        color: rgb(41, 121, 255);
        font-weight: bold;
      }
      /deep/.van-tabs__line {
        width: 30px;
        background-color: rgb(41, 121, 255);
        height: 3px;
        border-radius: 3px;
      }
    }
    .van-search {
      padding-top: 0;
    }
    ::-webkit-scrollbar {
      width: 0 !important ;
      height: 0 !important;
    }
    .datasBox {
      height: 525px;
      overflow: scroll;
      .datas {
        padding: 20px;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
